/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lk.mrt.msc.swa.epid.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JSpinner;
import lk.mrt.msc.swa.epid.db.MySQLAccess;

/**
 *
 * @author Nadesh
 */
public class AddIncident extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * This method adds the incident details to the Database and update it. The necessary 
     * patameters are taking from request session.
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        final PrintWriter out = response.getWriter();
        try{
        Vector vect = new Vector();
        
        final HttpSession session = request.getSession(true);
        final String hospital = (String)session.getAttribute("hospital");
        vect.add(0, hospital);
        
        final String caseno = (String)session.getAttribute("caseno");
        vect.add(1, caseno);
        
        final String name = (String)session.getAttribute("name");
        vect.add(2, name);
        
        final String d1 = (String)session.getAttribute("d1");String d2 = (String)session.getAttribute("d2");
        final int day = Integer.parseInt(d1+d2);
        vect.add(3, new Integer(day));
        
        final String m1 = (String)session.getAttribute("m1");String m2 = (String)session.getAttribute("m2");
        final int month = Integer.parseInt(m1+m2);
        vect.add(4, new Integer(month));
        
        final String y1 = (String)session.getAttribute("y1");String y2 = (String)session.getAttribute("y2");
        final String y3 = (String)session.getAttribute("y3");String y4 = (String)session.getAttribute("y4");
        final int year = Integer.parseInt(y1+y2+y3+y4);
        vect.add(5, year);
        
        final String sex = (String)session.getAttribute("sex");
        vect.add(6, sex);
        
        final String ethnicity = (String)session.getAttribute("ethnicity");
        vect.add(7, ethnicity);
        
        final String occupation = (String)session.getAttribute("occupation");
        vect.add(8, occupation);
        
        final String address = (String)session.getAttribute("address");
        vect.add(9, address);
        
        final String district = (String)session.getAttribute("district");
        vect.add(10, district);
        
        final String od1 = request.getParameter("od1");String od2 = request.getParameter("od2");
        final int oday = Integer.parseInt(od1+od2);
        vect.add(11, new Integer(oday));
        
        final String om1 = request.getParameter("om1");String om2 = request.getParameter("om2");
        final int omonth = Integer.parseInt(om1+om2);
        vect.add(12, new Integer(omonth));
        
        final String oy1 = request.getParameter("oy1");String oy2 = request.getParameter("oy2");
        final String oy3 = request.getParameter("oy3");String oy4 = request.getParameter("oy4");
        final int oyear = Integer.parseInt(oy1+oy2+oy3+oy4);
        vect.add(13, new Integer(oyear));
        
        final String treated = request.getParameter("treated");
        vect.add(14, treated);
        
        final String ad1 = request.getParameter("ad1");String ad2 = request.getParameter("ad2");
        final int aday = Integer.parseInt(ad1+ad2);
        vect.add(15, new Integer(aday));
        
        final String am1 = request.getParameter("am1");String am2 = request.getParameter("am2");
        final int amonth = Integer.parseInt(am1+am2);
        vect.add(16, new Integer(amonth));
        
        final String ay1 = request.getParameter("ay1");String ay2 = request.getParameter("ay2");
        final String ay3 = request.getParameter("ay3");String ay4 = request.getParameter("ay4");
        final int ayear = Integer.parseInt(ay1+ay2+ay3+ay4);
        vect.add(17, new Integer(ayear));
        
        final String ward = request.getParameter("ward");
        vect.add(18, ward);
        
        final String bht = request.getParameter("bht");
        vect.add(19, bht);
        
        final String outcome = request.getParameter("outcome");
        vect.add(20, outcome);
        
        final String dd1 = request.getParameter("dd1");String dd2 = request.getParameter("dd2");
        final int dday = Integer.parseInt(dd1+dd2);
        vect.add(21, new Integer(dday));
        
        final String dm1 = request.getParameter("dm1");String dm2 = request.getParameter("dm2");
        final int dmonth = Integer.parseInt(dm1+dm2);
        vect.add(22, new Integer(dmonth));
        
        final String dy1 = request.getParameter("dy1");String dy2 = request.getParameter("dy2");
        final String dy3 = request.getParameter("dy3");String dy4 = request.getParameter("dy4");
        final int dyear = Integer.parseInt(dy1+dy2+dy3+dy4);
        vect.add(23, new Integer(dyear));
        
        
        final String submit = request.getParameter("submit");
        
        if(submit.equals("Submit")){
            MySQLAccess mySqlAccess = new MySQLAccess();
            boolean status = mySqlAccess.addIncident(vect);
            request.setAttribute("status", status);            
        }
        
        session.invalidate();
        response.sendRedirect("/EpidStats/index.jsp");
        
        
        
        System.out.println(hospital +":"+caseno +":"+day +":"+month +":"+year +":"+sex+":"+ethnicity+":"+occupation+":"+address+":"+district);
        System.out.println(oday +":"+omonth +":"+oyear +":"+sex+":"+treated+":"+aday +":"+amonth +":"+ayear +ward+":"+bht+":"+outcome+":"+dday +":"+dmonth +":"+dyear+":"+submit);
        }catch(Exception e){
            
        }  finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
